/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.model.Color;
import com.techshop.model.Phone;
import com.techshop.model.PhoneCode;
import com.techshop.model.Promotion;
import com.techshop.service.PhoneCodeService;
import com.techshop.service.PhoneService;
import com.techshop.service.PromotionService;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "promotionController")
@SessionScoped
public class PromotionController extends AbstractController
{
    Promotion selectedPromotion;
    private List<Promotion> promotionList;
    private List<Promotion> filterPromotions;
    private boolean isSearch = false;
    
    @ManagedProperty(value = "#{promotionService}")
    private PromotionService promotionService;

    private List<Phone> phoneList;
    private List<PhoneCode> phoneCodeList;
    
    @ManagedProperty(value = "#{phoneService}")
    private PhoneService phoneService;
    @ManagedProperty(value = "#{phoneCodeService}")
    private PhoneCodeService phoneCodeService;

    @PostConstruct
    public void init()
    {
        resetSelectedPromotion();
    }
    
    public PhoneService getPhoneService() {
        return phoneService;
    }

    public void setPhoneService(PhoneService phoneService) {
        this.phoneService = phoneService;
    }

    public PhoneCodeService getPhoneCodeService() {
        return phoneCodeService;
    }

    public void setPhoneCodeService(PhoneCodeService phoneCodeService) {
        this.phoneCodeService = phoneCodeService;
    }
    
    public void insert()
    {
        isSearch = false;
        selectedPromotion.getPhoneCode().setId(phoneCodeService.getByPhoneAndColor(selectedPromotion.getPhoneCode().getPhone().getId(), selectedPromotion.getPhoneCode().getColor().getId()).getId());
        if (promotionService.insert(selectedPromotion))
        {
            addInfoMessage("Thêm thành công");
            resetSelectedPromotion();
        }
        else
        {
            addErrorMessage("Thêm thất bại");
        }
    }

    public void update()
    {
        isSearch = false;
        selectedPromotion.getPhoneCode().setId(phoneCodeService.getByPhoneAndColor(selectedPromotion.getPhoneCode().getPhone().getId(), selectedPromotion.getPhoneCode().getColor().getId()).getId());
        if (promotionService.update(selectedPromotion))
        {
            addInfoMessage("Cập nhật thành công");
        }
        else
        {
            addErrorMessage("Cập nhật thất bại");
        }
    }
    
    public void delete()
    {
        isSearch = false;
        if (promotionService.delete(selectedPromotion))
        {
            addInfoMessage("Xóa thành công");
        }
        else
        {
            addErrorMessage("Xóa thất bại");
        }
    }
    
    public void search()
    {
        isSearch = true;
        //setPromotionList(promotionService.search(selectedStore.getId(), selectedQuantity));
    }
    
    public void resetSelectedPromotion()
    {
        selectedPromotion = new Promotion();
        selectedPromotion.setPhoneCode(new PhoneCode());
        selectedPromotion.getPhoneCode().setPhone(new Phone());
        selectedPromotion.getPhoneCode().setColor(new Color());
    }

    public List<Promotion> getPromotionList()
    {
        if (!isSearch)
        {
            setPromotionList(promotionService.getAll());
        }
        return promotionList;
    }

    public void setPromotionList(List<Promotion> promotionList)
    {
        this.promotionList = promotionList;
    }

    public PromotionService getPromotionService()
    {
        return promotionService;
    }

    public void setPromotionService(PromotionService promotionService)
    {
        this.promotionService = promotionService;
    }

    public Promotion getSelectedPromotion()
    {
        selectedPromotion.setPhone(selectedPromotion.getPhoneCode().getPhone());
        return selectedPromotion;
    }

    public void setSelectedPromotion(Promotion selectedPromotion)
    {
        this.selectedPromotion = selectedPromotion;
    }

    public List<Promotion> getFilterPromotions()
    {
        return filterPromotions;
    }

    public void setFilterPromotions(List<Promotion> filterPromotions)
    {
        this.filterPromotions = filterPromotions;
    }
    
    public List<Phone> getPhoneList() {
        setPhoneList(phoneService.getAll());
        return phoneList;
    }

    public void setPhoneList(List<Phone> phoneList) {
        this.phoneList = phoneList;
    }

    public List<PhoneCode> getPhoneCodeList() {
        try
        {
            setPhoneCodeList(phoneCodeService.getByPhoneId(selectedPromotion.getPhoneCode().getPhone().getId()));
        }
        catch (NullPointerException e)
        {
        }
        return phoneCodeList;
    }

    public void setPhoneCodeList(List<PhoneCode> phoneCodeList) {
        this.phoneCodeList = phoneCodeList;
    }
}